package com.readfeed.app.adapter;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.readfeed.app.R;
import com.readfeed.app.model.Channel;

public class ChannelsListAdapter extends ArrayAdapter<Channel> {

	private List<Channel> mDisplayChannels;
	private List<Channel> mSelectedChannels;
	private SparseBooleanArray mSelection;
	private Activity mContext;
	
	public ChannelsListAdapter(Activity context, int resourceId, List<Channel> channels) {
		super(context, resourceId, channels);
		this.mDisplayChannels = channels;		
		this.mContext = context;
		this.mSelection = new SparseBooleanArray();
		this.mSelectedChannels = new ArrayList<Channel>();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		
		if (convertView == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			convertView = inflater.inflate(R.layout.channel_row, null);
			
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) convertView.findViewById(R.id.channel_title);
			viewHolder.feedsCount = (TextView) convertView.findViewById(R.id.items_count_text);
			viewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.channel_arrow_icon);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		Channel channel = this.mDisplayChannels.get(position);				
		if (channel != null) {
			viewHolder.title.setText(channel.getTitle());
			viewHolder.feedsCount.setText("Feeds:(" + channel.getItems().size() + ")");
		}
		
		if (mSelection.get(position)) {
			viewHolder.rightArrow.setImageResource(R.drawable.ic_check);
		} else {
			viewHolder.rightArrow.setImageResource(R.drawable.ic_right_arrow);
		}
		
		return convertView;		
	}
	
	public void addChannel(Channel channel){
		if (channel != null) {
			this.mDisplayChannels.add(channel);
			notifyDataSetChanged();					
		}		
	}
	
	public void removeChannel(int pos){
		this.mDisplayChannels.remove(pos);
		notifyDataSetChanged();
	}
	
	public Channel getChannelByPosition(int position){
		return mDisplayChannels.get(position);
	}
	
	public List<Channel> getSelectedChannels(){
		return mSelectedChannels;
	}
	
	public void removeSelectedChannels(){
		for (int i = 0; i < mSelectedChannels.size(); i++) {
			Channel channelToRemove = mSelectedChannels.get(i);
			mDisplayChannels.remove(channelToRemove);
		}
		
		notifyDataSetChanged();
	}
	
	public void setNewSelection(int position, boolean value){
		this.mSelection.put(position, value);
		Channel channel = mDisplayChannels.get(position);
		mSelectedChannels.add(channel);
		notifyDataSetChanged();
	}
	
	public boolean isPositionChecked(int position){
		Boolean result = this.mSelection.get(position);
		if (result == null) {
			return false;
		}
		
		return result;
	}
	
	public void removeSelection(int position){
		this.mSelection.delete(position);
		Channel channel = mDisplayChannels.get(position);
		mSelectedChannels.remove(channel);
		notifyDataSetChanged();
	}
	
	public void clearSelection(){
		this.mSelection = new SparseBooleanArray();
		mSelectedChannels = new ArrayList<Channel>();
		notifyDataSetChanged();
	}
	
	private static class ViewHolder{
		TextView title;
		TextView feedsCount;
		ImageView rightArrow;
	}	
}
